package com.polaris.live.ui.message.widget.single_message

import android.view.View
import com.polaris.live.adapter.MessageAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.util.AppCodeUtils.arTo
import com.polaris.live.databinding.MessageItemTextBinding
import com.polaris.live.im.db.entity.MessageEntity

/**
 * 文本消息逻辑 ui 等等的处理
 *
 * @author Created by 半仙 on 2023/9/4/004 13:40
 */
class MessageTextItemHolder(
    adapter: MessageAdapter,
    binding: MessageItemTextBinding
) : AbstractMessageItemHolder<MessageItemTextBinding>(adapter, binding) {

    override fun bind(item: MessageEntity) {
        mBinding.apply {
            bubbleLeft.bingTranslation(
                ivMessageTranslate,
                messageTextLeftTranslateLine,
                messageTextLeftTranslateLineRight
            )

            bindCommon(
                item,
                layoutTime.messageTextTime,
                layoutLeft,
                layoutRight,
                layoutLeftAvatar.avAvatar,
                layoutRightAvatar.avAvatar,
                layoutLeftAvatar.avFv,
                layoutRightAvatar.avFv,
                bubbleLeft,
                bubbleRight,
                ivState,
                tvError
            )
        }
        if (item.sender == UserManager.getUser().userId) {
            setTextRight(item)
        } else {
            setTextLeft(item)
        }
    }

    private fun setTextLeft(item: MessageEntity) {
        mBinding.apply {
            messageTextLeftText.text = item.content.arTo()
            if (item.translateContent.isNullOrEmpty()) {
                groupMessageTranslate.visibility = View.GONE
                messageTextLeftTranslateLine.visibility = View.GONE
                messageTextLeftTranslateLineRight.visibility = View.GONE
                messageTextLeftTextTranslate.visibility = View.GONE
            } else {
                groupMessageTranslate.visibility = View.VISIBLE
                messageTextLeftTranslateLine.visibility = View.VISIBLE
                messageTextLeftTranslateLineRight.visibility = View.VISIBLE
                messageTextLeftTextTranslate.visibility = View.VISIBLE
                messageTextLeftTextTranslate.text = item.translateContent
            }
        }
    }

    private fun setTextRight(item: MessageEntity) {
        mBinding.apply {
            rightContentText.text = item.content.arTo()
        }
    }
}